
package com.noah.project.dao

import com.noah.project.model.Comment
import com.noah.project.model.ReplyComment
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository


@Repository
interface CommentDao {

    @Insert("INSERT INTO comment(nid, uid, msg, cTime) VALUES(#{nid}, #{uid}, #{msg}, #{cTime})")
    @Options(useGeneratedKeys = true, keyProperty = "bid", keyColumn = "bid")//可以返回当前的uid,不加这行返回的是插入的行数
    fun insert(comment: Comment): Long

    @Delete("DELETE FROM comment WHERE cid =#{cid}")
    fun delete(cid: Long?): Long

    @Update("UPDATE comment SET msg = #{msg}  WHERE cid = #{cid}")
    fun update(comment: Comment): Long

    @Select("SELECT * FROM comment WHERE cid = #{cid}")
    fun findById(cid: Long?): Comment?

    @Select("SELECT * FROM comment WHERE cid = #{cid}")
    @Results(
            Result(column = "uid", property = "user", one = One(select = "com.noah.project.dao.UserDao.findReplyById")))
    fun findComment(cid: Long): ReplyComment? //Reply返回类,必须要和数据库里面的字段一样,需要什么返回几个参数就写几个参数,联合查询的user,Reply返回类也需要有

    @Select("SELECT * FROM comment WHERE nid = #{nid} AND cTime < #{time} order by cid desc LIMIT 10")
    @Results(
            Result(column = "uid", property = "user", one = One(select = "com.noah.project.dao.UserDao.findReplyById")))
    fun findCommentList(nid: Long?, time: Long?): List<ReplyComment>

    @Select("SELECT * FROM comment")
    fun findAll(): List<Comment>
}

/**
 * java写法
@Select("SELECT * FROM comment WHERE cid = #{cid}")
@Results({
@Result(id = true, column = "stud_id", property = "studId"),
@Result(column = "name", property = "name"),
@Result(column = "email", property = "email"),
@Result(property = "address", column = "addr_id",
one = @One(select = "com.noah.project.dao.UserDao.findReplyById"))
})
ReplyComment findComment(int studId);
 */
